home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / mktemp.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  2KB  |  72 lines

  1. /* From the TOS GCC library by jrd */
  2. /* modified to accept only template with trailing XXX's (really should reqire
  3.  * that there be six trailing X's)
  4.  */
  5.  
  6. #include <stddef.h>
  7. #include <support.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <assert.h>
  11. #include <errno.h>
  12. #include "lib.h"
  13.  
  14. extern int __mint;        /* 0 for TOS, MiNT version number otherwise */
  15.  
  16. #define TEN_MUL(X)    ((((X) << 2) + (X)) << 1)
  17.  
  18. char * mktemp(pattern)
  19. char * pattern;
  20. {
  21.   char * p, * q;
  22.   long tempnum, nx;
  23.   static int startat = 0;
  24.   int save_errno;
  25.  
  26.   assert((pattern != NULL));
  27.  
  28.   /* scan back over X's */
  29.   for(p = pattern; *p; p++) ;
  30.   for(q = p; q[-1] == 'X'; --q) ;
  31.   if((nx = p - q) == 0)  /* # of X's */
  32.     return NULL;
  33.  
  34.   /* if MiNT is active and there's room, put in the pid */
  35.   /* we need 5 X's for this: up to 3 for the pid, and up to 2 for the
  36.      extra number */
  37.   if (__mint && nx > 4 && startat < 256) {
  38.     (void) _itoa(getpid(), q, 10);
  39.     while (*q) q++;
  40.     /* be sure to generate each name only once */
  41.     if (startat < 16) *q++ = '0';
  42.     (void) _itoa(startat++, q, 16);
  43.     while (*q) q++;
  44.     while (q < p) *q++ = '0'; /* fill with zeros */
  45.     return pattern;
  46.   }
  47.  
  48.   /* calc the #'s to try for X's, for 2 X's 10-99 and so on */
  49.   for(tempnum = 1; --nx > 0; tempnum = TEN_MUL(tempnum)) ; /* [lower */
  50.   nx = TEN_MUL(tempnum);                  /* upper) */
  51.   tempnum += startat;  /* dont always start at [lower, start at lower+startat */
  52.   if(tempnum >=nx )
  53.   {
  54.       tempnum -= startat;
  55.       startat = 0;
  56.   }
  57.   else 
  58.       startat++;
  59.   save_errno = errno;
  60.   for(; tempnum < nx; tempnum++)
  61.   {
  62.     (void) _ltoa(tempnum, q, 10); /* assumption: strrev reverses in place */
  63.     if(access(pattern, F_OK))    /* using access takes care of unx2dos also */
  64.     {
  65.     errno = save_errno;
  66.     return pattern;
  67.     }
  68.   }
  69.   errno = save_errno;
  70.   return NULL;
  71. }
  72.